package org.teamh.gui.engine.effects;

import java.util.Random;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.effects.EffectImpl;
import de.lessvoid.nifty.effects.EffectProperties;
import de.lessvoid.nifty.effects.Falloff;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.render.NiftyRenderEngine;

/**
 * This is the squizzle effect for the nifty-gui. This effect makes the selected
 * element to squizzle over the screen for a certain time.
 * 
 * @author richard
 * 
 */
public class SquizzleEffect implements EffectImpl {

	private Random random = new Random();

	private int offset;
	
	private Element element;

	@Override
	public void activate(Nifty nifty, Element element,
			EffectProperties properties) {
		offset = Integer.parseInt(properties.getProperty("offset"));
		this.element = element;
	}

	@Override
	public void deactivate() {
		requestFocusForFirstElement(element);
	}

	@Override
	public void execute(Element element, float normalizedTime, Falloff falloff,
			NiftyRenderEngine renderEngine) {
		renderEngine.moveTo(-offset / 2 + random.nextInt(offset), -offset / 2
				+ random.nextInt(offset));
	}

	/**
	 * This request must be called due to an error within nifty. This does not
	 * make it reusable, but it should do for now.
	 * 
	 * @param element
	 */
	private void requestFocusForFirstElement(Element element) {
		element.findNiftyControl("eingabeName", TextField.class).setFocus();
		element.findNiftyControl("eingabePasswort", TextField.class).setFocus();
		element.findNiftyControl("eingabeName", TextField.class).setFocus();
	}
}
